package com.lgl.allpeoplevent.ventIndex.controller;

import com.github.pagehelper.PageInfo;
import com.lgl.allpeoplevent.base.controller.BaseController;
import com.lgl.allpeoplevent.system.service.*;
import com.lgl.allpeoplevent.ventIndex.service.VentCommentService;
import com.lgl.allpeoplevent.ventIndex.service.VentService;
import com.lgl.allpeoplevent.ventIndex.service.VentUserService;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Tools;
import com.lgl.scsapi.vent.model.Vent;
import com.lgl.scsapi.vent.model.VentComment;
import com.lgl.scsapi.vent.model.VentUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统前台部分首页部分
 */
@Slf4j
@RestController
@RequestMapping(value="/ventIndex")
public class VentIndexController extends BaseController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private DictionariesService dictionariesService;
    @Autowired
    private VentService ventService;
    @Autowired
    private VentCommentService ventCommentService;
    @Autowired
    private VentUserService ventUserService;
    @Autowired
    private UserCountService userCountService;
    @Autowired
    private PutADService putADService;
    @Autowired
    private VentFeedbackService ventFeedbackService;

    /**
     * 获取数据字典集合
     */
    @RequestMapping(value="/listDictionaries")
    public Object listDictionaries(){
        return dictionariesService.listDictionaries(getPageData());
    }
    /**
     * 获取所有城市集合
     */
    @RequestMapping(value="/listAllCities")
    public Object listAllCities(){
        return addressService.listAllCities(getPageData());
    }
    /**
     * 获取吐槽分页对象（index、hisHomePage页面使用）
     */
    @RequestMapping(value="/listIndexPageVent")
    public PageInfo listIndexPageVent(){
        PageData ventParamPd = getPageData();
        int pageNo = ventParamPd.getInt("pageNo"),pageSize = ventParamPd.getInt("pageSize");
        Tools.initPageNoAndSize(pageNo,pageSize);
        //VentUser loginUser = getSessionUser();
        //过滤掉仅自己可见、被注销的吐槽数据
        ventParamPd.put("ventStatus","0");
        //ventParamPd.put("loginUserId", loginUser == null ? "" : loginUser.getUserId());
        return ventService.pageIndexVent(pageNo,pageSize,ventParamPd);
    }
    //获取一条吐槽数据
    @RequestMapping(value="/getIndexVent")
    public Object getIndexVent(){
        PageData pageData = getPageData();
        VentUser ventUser = getSessionUser();
        pageData.put("likeUserId", ventUser == null ? "" : ventUser.getUserId());
        Vent v = ventService.getIndexVent(ventUser,pageData);
        pageData.clear();
        pageData.put("ventObj", v);
        return pageData;
    }
    //获取吐槽下的一级评论数据集合（同时会查询出对应的二级评论）
    @RequestMapping(value="/listOneLevelComment")
    public Object listOneLevelComment(){
        PageData pageData = getPageData();
        int pageNo = pageData.getInt("pageNo"), pageSize = pageData.getInt("pageSize");
        Tools.initPageNoAndSize(pageNo,pageSize);
        VentUser ventUser = getSessionUser();
        pageData.put("likeUserId", ventUser == null ? "" : ventUser.getUserId());
        PageInfo piVentComment = ventCommentService.listOneLevelComment(pageNo, pageSize,pageData);
        List<VentComment> oneLevelCommentList = piVentComment.getList();
        //如果一级评论为空
        if (oneLevelCommentList == null || oneLevelCommentList.isEmpty()){
            return piVentComment;
        }
        VentComment firstComment = oneLevelCommentList.get(0);
        //如果总回复数小于1
        if (firstComment.getReplyTotal() < 1){
            return piVentComment;
        }
        pageData.put("parentCommentId", firstComment.getId());
        List<VentComment> twoLevelCommentList = ventCommentService.listTwoLevelComment(pageData);
        pageData.clear();
        firstComment.setTwoLevelCommentList(twoLevelCommentList);
        return piVentComment;
    }
    //获取吐槽下的二级评论数据集合
    @RequestMapping(value="/listTwoLevelComment")
    public Object listTwoLevelComment(){
        PageData commentPd = getPageData();
        VentUser ventUser = getSessionUser();
        commentPd.put("likeUserId", ventUser == null ? "" : ventUser.getUserId());
        return ventCommentService.listTwoLevelComment(commentPd);
    }
    //获取点赞量、阅读量、评论量最多的关注（根据吐槽对象、吐槽职业、吐槽人来进行猜测）的吐槽数据
    @RequestMapping(value="/listFollowVentByMost")
    public Object listFollowVentByMost(){
        PageData mostPd = getPageData();
        List<PageData> mostFollowList = ventService.listFollowVentByMost(mostPd);
        if (mostFollowList == null){
            mostFollowList = new ArrayList<>(18);
        }
        int pageSize = 9, mostFollowSize = mostFollowList.size();
        if (mostFollowSize >= pageSize){
            return mostFollowList;
        }
        //mostList中要去掉mostFollowList里的吐槽数据，以免出现重复
        if (mostFollowSize > 0){
            Object notVentIdObj = mostPd.remove("notVentId");
            int notVentId = notVentIdObj == null ? 0 : Integer.parseInt(notVentIdObj.toString());
            Integer[] notVentIdArr = new Integer[mostFollowSize + 1];
            notVentIdArr[0] = notVentId;
            for (int g = 0; g < mostFollowSize; g ++){
                notVentIdArr[g + 1] = mostFollowList.get(g).getInt("id");
            }
            mostPd.put("notVentIdArr", notVentIdArr);
        }
        mostPd.put("pageSize", pageSize - mostFollowList.size());
        mostPd.put("ventFields","v.id, v.title, v.short_content as shortContent");
        List<PageData> mostList = ventService.listMostLikeReadComment(mostPd);
        mostFollowList.addAll(mostList);
        return mostFollowList;
    }
    //推荐更多吐槽
    @RequestMapping(value="/recommendMoreVent")
    public PageInfo recommendMoreVent(){
        PageData ventParamPd = getPageData();
        int pageNo = ventParamPd.getInt("pageNo"),pageSize = ventParamPd.getInt("pageSize");
        Tools.initPageNoAndSize(pageNo,pageSize);
        return ventService.pageRecommendMoreVent(pageNo,pageSize,ventParamPd);
    }
    //获取“他的主页”中的用户信息
    @RequestMapping(value="/getHisInfo")
    public VentUser getHisInfo(){
        return ventUserService.getHisInfo(getPageData(),true);
    }
    /**
     * 获取"他的主页"吐槽分页对象
     */
    @RequestMapping(value="/pageHisVent")
    public PageInfo pageHisVent() {
        PageData ventParamPd = getPageData();
        int pageNo = ventParamPd.getInt("pageNo"),pageSize = ventParamPd.getInt("pageSize");
        Tools.initPageNoAndSize(pageNo,pageSize);
        //过滤掉仅自己可见、被注销的吐槽数据
        ventParamPd.put("ventStatus","0");
        return ventService.pageIndexVent(pageNo,pageSize,ventParamPd);
    }
    //吐槽阅读量加1
    @RequestMapping(value="/readTotalAddOne")
    public void readTotalAddOne(@RequestParam String ventId) {
        ventService.readTotalAddOne(ventId);
    }
    //某用户主页访问量加1
    @RequestMapping(value="/hisHomeAccessAddOne")
    public void hisHomeAccessAddOne(@RequestParam String userId) {
        PageData homePd = new PageData(6);
        homePd.put("userId", userId);
        homePd.put("homeAccessNum","");
        int homeRows = userCountService.homeSiteTotalAddOne(homePd);
        //如果更新行数小于0，则说明数据库中还没有，则重新添加
        if (homeRows < 1){
            homePd.put("accessSiteNum",0);
            homePd.put("homeAccessNum",1);
            userCountService.saveUserCount(homePd);
        }
    }
    //某用户访问本网站的次数加1
    @RequestMapping(value="/accessSiteAddOne")
    public void accessSiteAddOne(@RequestParam String userId) {
        PageData homePd = new PageData(6);
        homePd.put("userId", userId);
        homePd.put("accessSiteNum","");
        int homeRows = userCountService.homeSiteTotalAddOne(homePd);
        //如果更新行数小于0，则说明数据库中还没有，则重新添加
        if (homeRows < 1){
            homePd.put("accessSiteNum",1);
            homePd.put("homeAccessNum",0);
            userCountService.saveUserCount(homePd);
        }
    }
    //通过关键字搜索吐槽对象集合
    @RequestMapping(value="/listVentToName")
    public Object listVentToName(){
        return ventService.listVentToName(getPageData());
    }
    //保存广告投放数据
    @RequestMapping(value="/savePutAD")
    public Object savePutAD(){
        PageData pageData = getPageData();
        int row = putADService.savePutAD(pageData);
        pageData.clear();
        pageData.put("row", row);
        return pageData;
    }
    //保存反馈数据
    @RequestMapping(value="/saveVentFeedBack")
    public Object saveVentFeedBack(){
        PageData pageData = getPageData();
        if (StringUtils.isEmpty(pageData.getString("fbContact"))){
            return 2;
        }
        return ventFeedbackService.saveVentFeedBack(pageData);
    }
}